﻿Imports System.Data.SqlClient

Public Class NghiemThuF

    Public Function GetListTrangThai(ByVal sotc As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_GetListTrangThai"

            .Parameters.Add(New SqlClient.SqlParameter("@sotc", SqlDbType.Char))
            .Parameters("@sotc").Value = sotc

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetListChietTinh(ByVal sotc As String, ByVal trangthai As Integer) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_GetListChietTinh"

            .Parameters.Add(New SqlClient.SqlParameter("@sotc", SqlDbType.Char))
            .Parameters("@sotc").Value = sotc

            .Parameters.Add(New SqlClient.SqlParameter("@trangthai", SqlDbType.Int))
            .Parameters("@trangthai").Value = trangthai

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetMaster(ByVal sotc As String, ByVal sophieu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_GetMaster"

            .Parameters.Add(New SqlClient.SqlParameter("@sotc", SqlDbType.Char))
            .Parameters("@sotc").Value = sotc

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = sophieu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetDetail(ByVal sophieu As String, ByVal maloaivattu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_GetDetail"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = sophieu

            .Parameters.Add(New SqlClient.SqlParameter("@maloaivattu", SqlDbType.Char))
            .Parameters("@maloaivattu").Value = maloaivattu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function TrangThaiGetAll() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_TrangThai_GetAll"

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function UpdateNghiemThu(ByVal SoPhieu As String, ByVal Ma As Integer, ByVal MaLoaiVatTu As String, ByVal SlNt As Decimal, ByVal ThuHoi As Boolean) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_UpdateNghiemThu"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Int))
            .Parameters("@ma").Value = Ma

            .Parameters.Add(New SqlClient.SqlParameter("@maloaivattu", SqlDbType.Char))
            .Parameters("@maloaivattu").Value = MaLoaiVatTu

            .Parameters.Add(New SqlClient.SqlParameter("@slnt", SqlDbType.Decimal))
            .Parameters("@slnt").Value = SlNt

            .Parameters.Add(New SqlClient.SqlParameter("@thuhoi", SqlDbType.Bit))
            .Parameters("@thuhoi").Value = ThuHoi

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function DeleteNghiemThu(ByVal SoPhieu As String, ByVal MaLoaiVatTu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_DeleteNghiemThu"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@maloaivattu", SqlDbType.Char))
            .Parameters("@maloaivattu").Value = MaLoaiVatTu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function UpdateTrangThai(ByVal sotc As String, ByVal sophieu As String, ByVal trangthai As Integer) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "NghiemThu_UpdateTrangThai"

            .Parameters.Add(New SqlClient.SqlParameter("@sotc", SqlDbType.Char))
            .Parameters("@sotc").Value = sotc

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = sophieu

            .Parameters.Add(New SqlClient.SqlParameter("@trangthai", SqlDbType.Int))
            .Parameters("@trangthai").Value = trangthai

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

End Class

